/*
 * trans.h
 *
 *  Created on: 2024年11月4日
 *      Author: zizhong
 */

#ifndef CONTROLLIB_TRANS_H_
#define CONTROLLIB_TRANS_H_

#define  on_by_sqrt3   0.57735026918963    /* 1/sqrt(3) */

typedef struct
{  float  Alpha;
// Input: stationary d-axis stator variable
   float  Beta;
   // Input: stationary q-axis stator variable
   float  Angle;
   // Input: rotating angle (pu)
   float  Ds;
   // Output: rotating d-axis stator variable
   float  Qs;
   // Output: rotating q-axis stator variable
   float  Sine;
   float  Cosine;
} TRANS_park_t;

#define PARK_DEFAULTS {   0, \
    0, \
    0, \
    0, \
    0, \
    0, \
    0  \
}

static inline void runPark(TRANS_park_t * in)
{
    in->Ds = (in->Alpha * in->Cosine) + (in->Beta * in->Sine);
    in->Qs = (in->Beta * in->Cosine) - (in->Alpha * in->Sine);
}

typedef struct
{  float  Alpha;
// Output: stationary d-axis stator variable
   float  Beta;
   // Output: stationary q-axis stator variable
   float  Angle;
   // Input: rotating angle (pu)
   float  Ds;
   // Input: rotating d-axis stator variable
   float  Qs;
   // Input: rotating q-axis stator variable
   float  Sine;
   // Input: Sine term
   float  Cosine;
   // Input: Cosine term
} TRANS_ipark_t;

#define IPARK_DEFAULTS {  0, \
   0, \
   0, \
   0, \
   0, \
   0, \
   0  \
}

static inline void runIPark(TRANS_ipark_t * in)
{
    in->Alpha = (in->Ds * in->Cosine) - (in->Qs * in->Sine);
    in->Beta = (in->Qs * in->Cosine) + (in->Ds * in->Sine);
}

typedef struct
{  float  As;
// Input: phase-a stator variable
   float  Bs;
   // Input: phase-b stator variable
   float  Cs;
   // Input: phase-c stator variable
   float  Alpha;
   // Output: stationary d-axis stator variable
   float  Beta;
   // Output: stationary q-axis stator variable
} TRANS_clarke_t;

#define CLARKE_DEFAULTS { 0, \
  0, \
  0, \
  0, \
  0, \
}

// Clarke transform (with 2 currents)
//==========================================
static inline void runClarkeTwoInput(TRANS_clarke_t * in)
{
    in->Alpha = in->As;
    in->Beta = ((in->As + 2*in->Bs) * on_by_sqrt3);
}

// Clarke transform (with 3 currents)
//==========================================
static inline void runClarkeThreeInput(TRANS_clarke_t * in)
{
    in->Alpha = in->As;
    in->Beta = ((in->Bs - in->Cs) * on_by_sqrt3);
}




#endif /* CONTROLLIB_TRANS_H_ */
